<?php

namespace apiend\modules\shop\models;

use Yii;
use common\base\Model;
use common\models\shop\ShopFavorite;

/**
 * 收藏、取消收藏商品模型
 * @author emhome <emhome@163.com>
 * @since 1.0
 */
class CollectForm extends Model {

    /**
     * @var integer 用户UID
     */
    public $user_id;

    /**
     * @var integer 商品ID
     */
    public $product_id = 0;

    /**
     * @var integer 店铺ID
     */
    public $store_id = 0;

    /**
     * @return array validation rules
     */
    public function rules() {
        return [
            [['user_id', 'product_id', 'store_id'], 'integer'],
            [['product_id', 'store_id'], 'default', 'value' => 0],
        ];
    }

    /**
     * 保存收藏的动态信息
     * @return boolean
     */
    public function save() {
        if (!$this->validate()) {
            return $this->dumpError('收藏失败');
        }
        if (!$this->product_id && !$this->store_id) {
            return $this->dumpError('收藏失败');
        }
        $model = ShopFavorite::findOrNew($this->attributes);
        if (!$model->isNewRecord) {
            return !$model->delete();
        }
        return $model->save();
    }

}
